package edu.esiag.isidis.fi.LBTest;


import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.Socket;

import edu.esiag.isidis.fi.lb.interfaces.IEntity;

public class NewMember implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8535440850698598230L;
	InetAddress  groupeIP;
	MulticastSocket socketEmission;
	String nom;
	EntityReciver reciver=null;

	public NewMember(InetAddress groupeIP, int port, String nom,String host) throws Exception {
		this.groupeIP = groupeIP;
		
		this.nom = nom;
		socketEmission = new MulticastSocket();
		socketEmission.joinGroup(groupeIP);
		reciver=new EntityReciver(nom, 10, host, port);
		emettre();
	}
	
	void emettre() throws Exception {
		byte[] contenuMessage;
		DatagramPacket adresse;
		ByteArrayOutputStream sortie = new ByteArrayOutputStream(); 
		ObjectOutputStream oos=new ObjectOutputStream(sortie);
		oos.writeObject(reciver);
		oos.flush();
		//(new DataOutputStream(sortie)).writeUTF(o); 
		contenuMessage = sortie.toByteArray();
		adresse = new DatagramPacket(contenuMessage, contenuMessage.length, groupeIP,reciver.getPort());
		socketEmission.send(adresse);
	}
}
